package com.zhongshengnetwork.rightbe.wzt.view;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.CircleTransform;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.gsonmodel.CircleLangModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;
import com.zhongshengnetwork.rightbe.lang.activity.LangPublishActivity;
import com.zhongshengnetwork.rightbe.lang.activity.UserLangActivity;
import com.zhongshengnetwork.rightbe.login.LoginActivity;

import java.util.HashMap;
import java.util.List;

public class WZTUserRecyclerAdapter extends RecyclerView.Adapter<WZTUserRecyclerAdapter.ViewHolder>{

    private List<CircleLangModel> list;
    public Context mContext;

    public WZTUserRecyclerAdapter(Context mContext, List<CircleLangModel>list){
        this.mContext = mContext;
        this.list = list;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{

        ImageView lang_circle_header;
        ImageView privateIcon;
        TextView lang_circle_nickname;
        Button lang_circle_care;
        TextView lang_circle_smart_value;
        TextView lang_circle_count;
        TextView lang_comment_count;
        TextView lang_circle_care_count;
        RelativeLayout lang_circle_item_layout;
        LinearLayout lang_circle_line;
        private ImageView member_icon;

        public ViewHolder(View view){
            super(view);
            lang_circle_header = (ImageView) view.findViewById(R.id.lang_circle_header);
            privateIcon = (ImageView) view.findViewById(R.id.private_icon);
            member_icon = (ImageView) view.findViewById(R.id.lang_member_icon);
            lang_circle_nickname = (TextView) view.findViewById(R.id.lang_circle_nickname);
            lang_circle_care = (Button) view.findViewById(R.id.lang_circle_care);
            lang_circle_smart_value = (TextView) view.findViewById(R.id.lang_circle_smart_value);
            lang_circle_count = (TextView) view.findViewById(R.id.lang_circle_count);
            lang_comment_count = (TextView) view.findViewById(R.id.lang_comment_count);
            lang_circle_care_count = (TextView) view.findViewById(R.id.lang_circle_care_count);
            lang_circle_item_layout = (RelativeLayout) view.findViewById(R.id.lang_circle_item_layout);
            lang_circle_item_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            lang_circle_line = (LinearLayout) view.findViewById(R.id.lang_circle_line);
            lang_circle_line.setBackgroundColor(AppThemeUtils.getInstance().getBackgroundColor());
            lang_circle_nickname.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        }

    }


    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lang_circle_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position)
    {
        holder.lang_circle_item_layout.setClickable(true);
        holder.lang_circle_item_layout.setTag(position);
        holder.lang_circle_item_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CircleLangModel model = list.get((int) view.getTag());
                Intent intent = new Intent(mContext, UserLangActivity.class);
                intent.putExtra(APIKey.useridKey,model.getUserid());
                intent.putExtra(APIKey.typeKey,1);
                intent.putExtra(APIKey.titleKey,model.getNickname());
                mContext.startActivity(intent);
            }
        });
        CircleLangModel model = list.get(position);

        Glide.with(mContext).load(model.getHeader()).transform(new CircleTransform(mContext)).dontAnimate().into(holder.lang_circle_header);

        if (!CommonUtils.isEmpty(model.getPrivateIcon())) {
            holder.privateIcon.setVisibility(View.VISIBLE);
            Glide.with(mContext).load(model.getPrivateIcon()).transform(new CircleTransform(mContext)).dontAnimate().into(holder.privateIcon);
        } else {
            holder.privateIcon.setVisibility(View.GONE);
        }

        holder.lang_circle_nickname.setText(model.getNickname());
        holder.lang_circle_care.setTag(position);
        if (model.iscare() == true) {
            holder.lang_circle_care.setVisibility(View.INVISIBLE);
        } else {
            holder.lang_circle_care.setVisibility(View.VISIBLE);
            holder.lang_circle_care.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickCare((int) v.getTag());
                }
            });
        }
        holder.lang_circle_smart_value.setText(model.getLevel() + "");
        holder.lang_circle_count.setText((model.getLangcount() - model.getLifecount()) + "");
        holder.lang_comment_count.setText(model.getWeishucount() + "");
        holder.lang_circle_care_count.setText(model.getFanscount() + "");

        if (!CommonUtils.isEmpty(model.getMemberIcon())){
            holder.member_icon.setVisibility(View.VISIBLE);
            try{
                Glide.with(mContext).load(model.getMemberIcon()).
                        diskCacheStrategy(DiskCacheStrategy.RESOURCE).fitCenter().centerCrop().into(holder.member_icon);
            }catch (Exception e){
                LangPublishActivity.uploadError("智慧排行点赞列表加载图片报错："+e.getLocalizedMessage());
            }

        }else {
            holder.member_icon.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return this.list.size();
    }


    //点击关注
    private void onClickCare(final int position) {
        if (mContext == null) {
            return;
        }
        if (CommonUtils.isLogin() == false) {
            Intent intent = new Intent(mContext, LoginActivity.class);
            mContext.startActivity(intent);
            return;
        }
        final CircleLangModel model = list.get(position);
        if (model.iscare() == true) {
            return;
        }
        model.setIscare(true);
        notifyItemChanged(position);

        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        map.put("authorid", model.getUserid());
        String url = "miniapp/" + "langappid" + "/addcare.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
            @Override
            public void onSuccess(String data) {
            }

            @Override
            public void onError(String msg) {
            }
        });

    }

}
